/*
 *
 * Copyright © 2007 Sun Microsystems, Inc. All rights reserved.
 * Use is subject to license terms.
 */
package project.platform.mms;

import java.io.IOException;

import javax.microedition.io.*;

import javax.wireless.messaging.*;

/**
 * Sun Microsystems' WMADemo SenderThread class.
 *
 */
public class SenderThread extends Thread {
    private MMSMessage message;
    private String appID;

    public SenderThread(final MMSMessage message, final String appID) {
        this.message = message;
        this.appID = appID;
    }

    /**
     * Send the message. Called on a separate thread so we don't have
     * contention for the display
     */
    public void run() {
        final String address = this.message.getDestination() + ":" + this.appID;

        MessageConnection mmsconn = null;

        try {
            /** Open the message connection. */
            mmsconn = (MessageConnection)Connector.open(address);

            final MultipartMessage mmmessage =
                (MultipartMessage)mmsconn.newMessage(MessageConnection.MULTIPART_MESSAGE);
            mmmessage.setAddress(address);

            final MessagePart[] parts = this.message.getParts();

            for (int i = 0; i < parts.length; i++)
				mmmessage.addMessagePart(parts[i]);

            mmmessage.setSubject(this.message.getSubject());
            mmsconn.send(mmmessage);
        } catch (final Exception e) {
            e.printStackTrace();
        }

        if (mmsconn != null)
			try {
                mmsconn.close();
            } catch (final IOException ioe) {
                ioe.printStackTrace();
            }
    }
}
